//
//  THJContractDetailVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/8/20.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJContractDetailVC.h"

@interface THJContractDetailVC ()<UIWebViewDelegate>
{
    UIWebView * webview ;
}
@end

@implementation THJContractDetailVC
//-(NSMutableArray*)infoArr{
//    if(!_infoArr){
//        _infoArr = [[NSMutableArray alloc] init];
//    }
//    return _infoArr;
//}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self SVHUDShow];
    NSLog(@"kk--%@",self.infoArr);
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"合同详情"];
    }else{
        [self setNavBarWithTitle:@"合同详情"];
    }
    webview = [[UIWebView alloc] initWithFrame:CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight - NavbarHeight)];
    [self.view addSubview:webview];
    NSURL *fileURL = [[NSBundle mainBundle] URLForResource:@"contract.html" withExtension:nil];
    webview.delegate = self;
    NSURLRequest *request = [NSURLRequest requestWithURL:fileURL];
    
    [webview loadRequest:request];
}
- (void)webViewDidFinishLoad:(UIWebView *)webView {
    [self SVHUDHiden];
    NSLog(@"KKKJJJJ-%@",self.infoArr);
     [self getData];
    //    NSString *headerStr = @"document.getElementsByTagName('h1')[0].innerText = '测试文字';";
    //    [webView stringByEvaluatingJavaScriptFromString:headerStr];
    NSString * str = [NSString stringWithFormat:@"document.getElementById('buyer').innerText = '%@';",self.infoArr[0]];
    [webView stringByEvaluatingJavaScriptFromString:str];
     NSString * str1 = [NSString stringWithFormat:@"document.getElementById('address').innerText = '%@';",self.infoArr[1]];
    [webView stringByEvaluatingJavaScriptFromString:str1];
    NSString * str2 = [NSString stringWithFormat:@"document.getElementById('faren').innerText = '%@';",self.infoArr[2]];
    [webView stringByEvaluatingJavaScriptFromString:str2];
    NSString * str3 = [NSString stringWithFormat:@"document.getElementById('delegate').innerText = '%@';",self.infoArr[3]];
    [webView stringByEvaluatingJavaScriptFromString:str3];
    NSString * str4 = [NSString stringWithFormat:@"document.getElementById('phone').innerText = '%@';",self.infoArr[4]];
    [webView stringByEvaluatingJavaScriptFromString:str4];
    NSString * str5 = [NSString stringWithFormat:@"document.getElementById('bank').innerText = '%@';",self.infoArr[5]];
    [webView stringByEvaluatingJavaScriptFromString:str5];
    NSString * str6 = [NSString stringWithFormat:@"document.getElementById('acount').innerText = '%@';",self.infoArr[6]];
    [webView stringByEvaluatingJavaScriptFromString:str6];
    NSString * str7 = [NSString stringWithFormat:@"document.getElementById('tax').innerText = '%@';",self.infoArr[7]];
    [webView stringByEvaluatingJavaScriptFromString:str7];
    NSString * str8 = [NSString stringWithFormat:@"document.getElementById('c_standard').innerText = '%@';",self.infoArr[8]];
    [webView stringByEvaluatingJavaScriptFromString:str8];
    NSString * str9 = [NSString stringWithFormat:@"document.getElementById('c_penalty').innerText = '%@';",self.infoArr[9]];
    [webView stringByEvaluatingJavaScriptFromString:str9];
    
}
-(void)getData{
    NSDictionary * dic = @{@"id":[RSA encryptString:self.orderId publicKey:RSA_PubKey],@"type":@"ios"};
    NSLog(@"tijiaoLL-%@",dic);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_buy_info" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"买家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self setContractWithDic:receiveData[@"data"]];
        }else{
//            [self HUDShowMsg:@"获取信息失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)setContractWithDic:(NSDictionary*)dic{
    NSArray * arr = @[
                      @"c_seller",
                      @"c_buyer",
                      @"c_date",
                      @"c_product_name",
                      @"c_gg",
                      @"c_unit",
                      @"c_amount",
                      @"c_tax",
                      @"c_total_money",
                      @"c_total_money_upper",
                      @"c_notes",
                      @"c_pack",
                      @"c_delivery_type",
                      @"c_freight_pay"
                      ];
    NSArray * arr2 = @[
                      [self DCRP_STR:dic[@"s_name"]],
                      [self DCRP_STR:dic[@"b_name"]],
                      [self timeChangeWithString:[self DCRP_STR:dic[@"addtime"]]],
                      [self DCRP_STR:dic[@"title"]],
                      [self DCRP_STR:dic[@"guige"]],
                      [self DCRP_STR:dic[@"unit"]],
                      [self DCRP_STR:dic[@"number"]],
                      [[self DCRP_STR:dic[@"hanshui"]] isEqual:@"1"]?@"含税":@"不含税",
                      [self DCRP_STR:dic[@"amount"]],
                      [self DCRP_STR:dic[@"damount"]],
                      dic[@"note"],
                      [[self DCRP_STR:dic[@"baozhuang"]] isEqual:@"1"]?@"吨小包": ([[self DCRP_STR:dic[@"baozhuang"]] isEqual:@"2"]?@"小袋":@"散装"),
                      [[self DCRP_STR:dic[@"yunshu"]] isEqual:@"1"]?@"包到":@"自提",
                      [[self DCRP_STR:dic[@"yunshu"]] isEqual:@"1"]?@"卖家":@"买家"
                      ];
    NSLog(@"循环-%@",[self DCRP_STR:dic[@"addtime"]]);
    for (NSInteger i  = 0; i<14; i++) {
        NSString * str1 = [NSString stringWithFormat:@"document.getElementById('%@').innerText = '%@';",arr[i],arr2[i]];
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }
    
    NSString * payType = [self DCRP_STR:dic[@"order_type"]];
    if([payType isEqual:@"A"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,发货全款解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else if([payType isEqual:@"B"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,装车分批解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else if([payType isEqual:@"C"]){
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,货到分批解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }else{
        NSString * str1 = @"document.getElementById('c_pay_type').innerText = '全款解冻,完成全部订单全款解付';";
        [webview stringByEvaluatingJavaScriptFromString:str1];
    }
    
    NSString * gg =  [THJTools ContraceGuigeTypeChangeToStrWithType:[self DCRP_STR:dic[@"guige"]]];
    NSString * ggC = [NSString stringWithFormat:@"document.getElementById('c_gg').innerText = '%@';",gg];
    [webview stringByEvaluatingJavaScriptFromString:ggC];
    NSString * orderidC = [NSString stringWithFormat:@"document.getElementById('c_number').innerText = '%@';",self.orderId];
    [webview stringByEvaluatingJavaScriptFromString:orderidC];

}
-(NSString*)DCRP_STR:(NSString*)str{
  return   [RSA decryptString:str publicKey:RSA_PubKey];
}
- (IBAction)sureContractAction:(id)sender {
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定合同内容无误并且订购吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了按钮1，进入按钮1的事件");
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        [self postInfo];
    }];
    
    
    
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    
    
    [self presentViewController:actionSheet animated:YES completion:nil];
   
}
-(void)postInfo{
    [self SVHUDShow];
    NSDictionary * dic = @{
                           @"type":@"ios",
                           @"itemid":[RSA encryptString:self.orderId publicKey:RSA_PubKey],
                           @"truename":[RSA encryptString:self.infoArr[0] publicKey:RSA_PubKey],
                           @"address":[RSA encryptString:self.infoArr[1] publicKey:RSA_PubKey],
                           @"faren":[RSA encryptString:self.infoArr[2] publicKey:RSA_PubKey],
                           @"dailiren":[RSA encryptString:self.infoArr[3] publicKey:RSA_PubKey],
                           @"tel":[RSA encryptString:self.infoArr[4] publicKey:RSA_PubKey],
                           @"accno":[RSA encryptString:self.infoArr[5] publicKey:RSA_PubKey],
                           @"yinhang":[RSA encryptString:self.infoArr[6] publicKey:RSA_PubKey],
                           @"shuihao":[RSA encryptString:self.infoArr[7] publicKey:RSA_PubKey],
                           @"yanshoubiaozhun":self.infoArr[8],
                           @"fine_standard":self.infoArr[9],
                           };
    NSLog(@"tijiaoLL-%@-orderid:%@",self.infoArr,self.orderId);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=hetong_buyer" parameters:dic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"买家合同信息--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
            [self HUDShowMsg:@"订购成功" AfterDelayDimiss:1];
            [self performSelector:@selector(popRoot) withObject:nil afterDelay:1];
        }else{
            [self HUDShowMsg:@"订购失败" AfterDelayDimiss:1];
        }
    }];
}
-(void)popRoot{
    [self.navigationController popToRootViewControllerAnimated:YES];
}
@end
